package com.example.riskmanage.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.riskmanage.bean.User;
import com.example.riskmanage.service.UserManager;

import java.util.HashMap;

@RestController
public class LoginController {
    String flag="error";
    User us=null;

    @Autowired
    UserManager userManager;


    @RequestMapping("/login")
    public String login(@RequestBody User user){


        us=userManager.loginService(user);
        if(us!=null){
            flag="ok";
        }else{
            flag="error";
        }


        HashMap<String,Object> res=new HashMap<String, Object>();//放入map集合K:V
        res.put("flag", flag);//收容标记flag 前是字符串后是字符串对象定义的hashmap
        res.put("user", us);
        String res_json= JSON.toJSONString(res);//把一个对象转换为json串:把收容有flag标记和student对象转变成json数据
        return res_json;//含有两块信息回应给前端
    }

    @RequestMapping("/register")
    public String register(@RequestBody User user){
        String flags=userManager.findByUserId(user);
        return flags;
    }
}
